package com.chenliang.swagger.utils;


import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: myes
 * @description:
 * @author: 陈英俊
 * @create: 2021-09-09 10:54
 */
public class JsonUtils {
    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private JsonUtils() {
    }

    public static String encode(Object obj) {
        String methodName = "encode(Object)";

        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonMappingException | JsonGenerationException var3) {
            logger.error(methodName, var3);
        } catch (IOException var4) {
            logger.error(methodName, var4);
        }

        return null;
    }

    public static <T> T decode(String json, Class<T> valueType) {
        String methodName = "decode(String, Class<T>)";

        try {
            return objectMapper.readValue(json, valueType);
        } catch (JsonMappingException | JsonParseException var4) {
            logger.error(methodName, var4);
        } catch (IOException var5) {
            logger.error(methodName, var5);
        }

        return null;
    }

    public static <T> T decode(String json, Class<T> valueType, String... paths) {
        String methodName = "decode(String, JsonTypeReference<T>,String ...paths)";
        ObjectMapper mapper = new ObjectMapper();

        try {
            JsonNode node = mapper.readTree(json);
            String[] var6 = paths;
            int var7 = paths.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String path = var6[var8];
                node = node.path(path);
            }

            if (node != null) {
                String nodeJson = mapper.writeValueAsString(node);
                return decode(nodeJson, valueType);
            }
        } catch (IOException var10) {
            logger.error("格式转换异常", var10);
        }

        return null;
    }

    public static <T> T decode(String json, TypeReference<T> typeReference) {
        String methodName = "decode(String, JsonTypeReference<T>)";

        try {
            return objectMapper.readValue(json, typeReference);
        } catch (JsonMappingException | JsonParseException var4) {
            logger.error(methodName, var4);
        } catch (IOException var5) {
            logger.error(methodName, var5);
        }

        return null;
    }

    public static <T> T decode(String json, TypeReference<T> typeReference, String... paths) {
        String methodName = "decode(String, JsonTypeReference<T>,String ...paths)";
        ObjectMapper mapper = new ObjectMapper();

        try {
            JsonNode node = mapper.readTree(json);
            String[] var6 = paths;
            int var7 = paths.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String path = var6[var8];
                node = node.path(path);
            }

            if (node != null) {
                String nodeJson = mapper.writeValueAsString(node);
                return decode(nodeJson, typeReference);
            }
        } catch (IOException var10) {
            logger.error("格式转换异常", var10);
        }

        return null;
    }

    public static void main(String[] args) {
        Map<String, String> paramMap = new HashMap();
        paramMap.put("aaa", "111");
        paramMap.put("bbb", "222");
        System.out.println(encode(paramMap));
    }

    static {
        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
}
